/***************************************************************************
 *   oboe.controllers.jml.JMLController                                    *
 *   Copyright (C) 2011 by Felipe Manga                                    *
 *   lufeboma@gmail.com                                                    *
 *                                                                         *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the       *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to    *
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:                                             *
 *                                                                         *
 *   The above copyright notice and this permission notice shall be        *
 *   included in all copies or substantial portions of the Software.       *
 *                                                                         *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
 ***************************************************************************/

package oboe.controllers.jml
{
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.Dictionary;
	
	import oboe.controllers.jml.helpers.JMLClipper;
	import oboe.controllers.jml.helpers.JMLForm;
	import oboe.controllers.jml.helpers.JMLInput;
	import oboe.controllers.jml.helpers.JMLNode;
	import oboe.controllers.jml.helpers.JMLPopUp;
	import oboe.controllers.jml.helpers.JMLSlideshow;
	import oboe.core.Controller;
	import oboe.resources.Files;
	
	public class JMLController extends Controller
	{
		private var idIndex:Dictionary;
		
		public var translation:Object;
		public var stylejml:Object;
		private var nodeTypes:Dictionary;
		private var dispObjTypes:Dictionary;
		
		public function JMLController( translation:Object=null, stylejml:Object=null )
		{
			super( );
			
			this.translation = translation;
			this.stylejml = stylejml;
			this.idIndex = new Dictionary(true);
			
			this.dispObjTypes = new Dictionary();
			dispObjTypes["Clipper"] = JMLClipper;
			dispObjTypes["Slideshow"] = JMLSlideshow;
			
			nodeTypes = new Dictionary();
			nodeTypes["JMLForm"] = JMLForm;
			nodeTypes["JMLInput"] = JMLInput;
			nodeTypes["JMLPopUp"] = JMLPopUp;
		}
		
		protected function loadStructure( jml:Object ):void
		{
			var rootNode:JMLNode;
			rootNode = new JMLNode( "", jml, this );
			this.addChild( rootNode );
		}
		
		protected function addControllerType( name:String, classObj:Class ):void
		{
			nodeTypes[name] = classObj;
		}
		
		protected function addDisplayObjectType( name:String, classObj:Class ):void
		{
			dispObjTypes[name] = classObj;
		}
		
		public function newIdIndex( id:String, node:JMLNode ):void
		{
			idIndex[ id ] = node;
		}
		
		public function getNodeFromId( id:String ):JMLNode
		{
			return idIndex[id];
		}
		
		public function getDisplayObjectForType( type:String ):DisplayObject
		{
			
			if( type.substr(0, 5) == "File$" )
			{
				var ex:Array = type.split("$");
				if( ex.length == 2 )
				{
					return new Bitmap( Bitmap(Files.getFile(ex[1])).bitmapData ); 
				}
				else if( ex.length == 3 )
				{
					return Files.getInstance(ex[1], ex[2]) as DisplayObject;
				}
				return new Sprite();
			}
			if( type == "Label" )
			{
					var tf:TextField = new TextField();
					tf.selectable = false;
					tf.autoSize = TextFieldAutoSize.LEFT;
					return tf;
			}
			if( type in this.dispObjTypes )
			{
				var c:Class = this.dispObjTypes[ type ];
				return new c();
			}
			return null;
		}
		
		public function getControllerForType( type:String, key:String, val:Object ):JMLNode
		{
			if( type in nodeTypes )
			{
				var c:Class = nodeTypes[type];
				return new c( key, val, this );
			}
			return new JMLNode(key, val, this); 
		}
	}
}
